import { usePublicHooks } from "../../hooks";
import { usePublicFormats } from "../../formats";

import { reactive, onMounted, h, toRaw } from "vue";
import { delay } from "@pureadmin/utils";
import { ReconciliationList, ReconciliationImport } from "@/api/reconciliation";
import { addDialog } from "@/components/ReDialog";
import { message } from "@/utils/message";
import editForm from "../form.vue";
import type { FormItemProps } from "../utils/types";

export function reconciliationConfig() {
  const form = reactive({
    parking_lot_park_code: "",
    parking_lot_name: "",
    reconciliation_created_at: "",
    reconciliation_status: -1,
    order_xiaoqiang_abnormal: -1,
    page: 1,
    limit: 20
  });

  //从父级钩子里取东西
  const { pagination, loadingConfig, dataList, formRef, loading } =
    usePublicHooks();

  const { orderStatusShow, orderTypeShow } = usePublicFormats();

  const columns: TableColumnList = [
    {
      label: "文件路径",
      prop: "file_path",
      width: 350
    },

    {
      label: "停车场名称",
      prop: "park_name",
      width: 200
    },
    {
      label: "对账类型",
      prop: "type",
      width: 100,
      cellRenderer: ({ row }) => {
        return orderTypeShow(row.type);
      }
    },
    {
      label: "状态",
      prop: "status",
      width: 100,
      cellRenderer: ({ row }) => {
        return orderStatusShow(row.status);
      }
    },
    {
      label: "上传时间",
      prop: "created_at",
      width: 200
    },
    {
      label: "操作",
      slot: "operation"
    }
  ];

  async function onSearch() {
    loading.value = true;
    const { data } = await ReconciliationList(toRaw(form));
    dataList.value = data.list;
    pagination.total = data.total;
    pagination.pageSize = data.pageSize;
    pagination.currentPage = data.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  //页面加载
  onMounted(() => {
    onSearch();
  });

  //页面条数变化
  function onSizeChange(val) {
    form.limit = val;
    onSearch();
  }

  //页码变化
  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    loading.value = true;
    delay(600).then(() => {
      form.page = val;
      onSearch();

      //刷新数据
      loading.value = false;
    });
  }

  //重置表单
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  function openDialog(title = "新增") {
    addDialog({
      width: "30%",
      title: "导入数据",
      contentRenderer: () => h(editForm, { ref: formRef }),
      props: {
        formInline: {}
      },
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`您新增了一条数据`, {
            type: "success"
          });
          done(); // 关闭弹框
          onSearch(); // 刷新表格数据
        }
        FormRef.validate(valid => {
          if (valid) {
            // 表单规则校验通过
            if (title === "新增") {
              ReconciliationImport(curData).then(() => {
                // 实际开发先调用新增接口，再进行下面操作
                chores();
              });
            }
          }
        });
      }
    });
  }

  return {
    columns,
    form,
    loading,
    onSearch,
    resetForm,
    dataList,
    pagination,
    onSizeChange,
    onCurrentChange,
    loadingConfig,
    openDialog
  };
}
